//index.js
//获取应用实例
const app = getApp();
Page({
    data: {
        list: [
            {title: '极速派单', cont: '周围三公里都可听单'},
            {title: '质量保障', cont: '平台认证装修人员'},
            {title: '需求针对', cont: '达到满意为止'},
        ],
        nav_list: [
            {name: '个人信息', img: '/assets/images/myIcon.png', path: 'Personal'},
            {name: '我的钱包', img: '/assets/images/himg3.png', path: 'balance'},
            {name: '优惠券', img: '/assets/images/himg4.png', path: 'Coupon'},
            {name: '装修计算器', img: '/assets/images/himg2.png', path: 'Calculator'},
            // {name: '说明书管理', img: '/assets/images/himg3.png', path: ''},
        ],
        hideModal: true,
        editNickname: '',
    },
    onLoad: function () {
        if(!app.globalData.userInfo) {
            this.getUserInfo();
        } else {
            this.setData({
                userInfo: app.globalData.userInfo,
                editNickname: app.globalData.userInfo.nickname
            })
        }
    },
    onHide: function () {
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.getUserInfo();
    },

    toPages: function (even) {
        let $dataset = even.currentTarget.dataset;
        if ($dataset.page == '') {
            wx.showToast({
                title: '即将开放',
                icon: 'none',
                duration: 2000
            });
            return
        }
        app.navigateTo(app.page[even.currentTarget.dataset.page]);
    },

    /**
     * 获取用户信息
     */
    getUserInfo: function(){
        var _this = this;
        var object = {
            url: app.api.url.user.userInfo,
            success: function(res){
                if(res.status == 1) {
                    app.globalData.userInfo = res.data;
                    _this.setData({
                        userInfo: res.data,
                        editNickname: res.data.nickname
                    })

                } else {
                    app.showToast(res.info);
                }
            },
            complete: function() {
                wx.stopPullDownRefresh() //停止下拉刷新
            }
        }
        app.request(object);
    },

    assistant: function() {
        var object = {
            url: app.api.url.assistant.service,
            data: {type: 4},
            success: function(res) {
                if(res.status == 1) {
                    app.navigateTo(app.page.wapUrl, 'url='+res.data.url);
                }
            }
        }
        app.request(object);
    },

    editInput: function(e){
        this.setData({
            editNickname: e.detail.value
        })
    },
    edit: function(){
        var nickname = this.data.userInfo.nickname;
        this.setData({
            hideModal: false,
            editNickname: nickname
        })
    },
    cancel: function(){
        this.setData({
            hideModal: true
        });
    },
    confirm: function(){
        var _this = this;
        var nickname = this.data.editNickname;
        var object = {
            url: app.api.url.user.infoEdit,
            data: {nickname: nickname},
            success: function(res) {
                app.showToast('修改成功');
                var userInfo = _this.data.userInfo;
                userInfo.nickname = nickname;
                _this.setData({
                    userInfo: userInfo,
                    hideModal: true
                })
            }
        }
        app.request(object);
    }
});
